package com.hgt.springcloud.controller;

import com.hgt.springcloud.common.CommonResult;
import com.hgt.springcloud.entity.Payment;
import com.hgt.springcloud.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @Author: huanggaotao
 * @Description:
 * @Date: Created in 12:46 2021/6/15
 * @Modified By:
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Autowired
    IPaymentService paymentService;

    @Value("${server.port}")
    private String SERVER_PORT;

    @PostMapping("/create")
    public CommonResult cretae(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("插入数据的ID：\t"+payment.getId());
        log.info("插入结果："+result);
        if (result>0) {
            return new CommonResult(200,"插入数据成功，serverport："+SERVER_PORT,result);
        }else {
            return new CommonResult(444,"插入数据失败",null);
        }

    }

    @GetMapping("/get/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("查询测试"+SERVER_PORT+"---------------------------------------------------------------");
        if (paymentById != null) {
            return new CommonResult(200,"查询成功"+"serverport:"+SERVER_PORT,paymentById);
        }else {
            return new CommonResult(400,"查询失败",null);
        }
    }

    @GetMapping("/payment/lb")
    public String getPayment(){
        return SERVER_PORT;
    }

    //超时测试
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return SERVER_PORT;
    }
}
